/**
 * Created on 12 avr. 08 by Sebastian Audet
 */
package grid;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Sebastian Audet
 * 
 */
public class TriangleGrid extends AbstractGrid {

    public boolean usesimple = true;
    
    /**
     * Sets whether or not to get all neighbors or just those linearly adjacent
     * @param b The boolean (<b>true</b>) to set this to use linearly adjacent,
     * (<b>false</b>) to set this to use all adjacent nodes.
     */
    public void setusesimple(boolean b)
    {
	usesimple=b;
    }

    public TriangleGrid(int x, int y) {
	super(x, y);
    }

    /*
     * (non-Javadoc)
     * 
     * @see grid.AbstractGrid#getNeighborNodesCollection(grid.Node)
     */
    @Override
    public Collection<Node> getNeighborNodesCollection(Node node) {

	Collection<Node> nodes = new ArrayList<Node>();
	Location loc = node.getLocation();
	ArrayList<Location> locs = new ArrayList<Location>();

	// LocationMap
	if ((loc.x % 2) == 0) {
	    if(usesimple)
	    {
		locs.add(new Location(loc.x - 1, loc.y));
		locs.add(new Location(loc.x + 1, loc.y));
		locs.add(new Location(loc.x, loc.y + 1));
	    }
	    else {
		locs.add(new Location(loc.x - 1, loc.y - 1));
		locs.add(new Location(loc.x, loc.y - 1));
		locs.add(new Location(loc.x + 1, loc.y - 1));
		locs.add(new Location(loc.x - 2, loc.y));
		locs.add(new Location(loc.x - 1, loc.y));
		locs.add(new Location(loc.x + 1, loc.y));
		locs.add(new Location(loc.x + 2, loc.y));
		locs.add(new Location(loc.x - 2, loc.y + 1));
		locs.add(new Location(loc.x - 1, loc.y + 1));
		locs.add(new Location(loc.x, loc.y + 1));
		locs.add(new Location(loc.x + 1, loc.y + 1));
		locs.add(new Location(loc.x + 2, loc.y + 1));
	    }
	} else {
	    if(usesimple) {
		locs.add(new Location(loc.x, loc.y - 1));
		locs.add(new Location(loc.x - 1, loc.y));
		locs.add(new Location(loc.x + 1, loc.y));
	    } else {
		locs.add(new Location(loc.x - 1, loc.y - 1));
		locs.add(new Location(loc.x, loc.y - 1));
		locs.add(new Location(loc.x + 1, loc.y - 1));
		locs.add(new Location(loc.x - 2, loc.y));
		locs.add(new Location(loc.x - 1, loc.y));
		locs.add(new Location(loc.x + 1, loc.y));
		locs.add(new Location(loc.x + 2, loc.y));
		locs.add(new Location(loc.x - 2, loc.y + 1));
		locs.add(new Location(loc.x - 1, loc.y + 1));
		locs.add(new Location(loc.x, loc.y + 1));
		locs.add(new Location(loc.x + 1, loc.y + 1));
		locs.add(new Location(loc.x + 2, loc.y + 1));
	    }
	}

	for (Location l : locs) {
	    try {
		nodes.add(getNode(l));
	    } catch (GridOutOfBounds e) {
		// Simply means it can't be added, move on
	    }
	}
	return nodes;
    }

    /*
     * (non-Javadoc)
     * 
     * @see grid.AbstractGrid#returnGridType()
     */
    @Override
    public int returnGridType() {
	return Grid.TRIANGLE;
    }

}
